package com.china.elbert.myapplication;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
    ObjectAnimator animator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView word = (TextView) findViewById(R.id.text);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                animator = ObjectAnimator.ofFloat(word, "zhy", 1.0f, 0.0f, 1.0f)
                        .setDuration(2000);
                animator.start();
                animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        float var = (float) animator.getAnimatedValue();
                        word.setAlpha(var);
                        word.setScaleX(var);
                    }
                });
            }
        });

        findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PropertyValuesHolder holder = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
                ObjectAnimator.ofPropertyValuesHolder(word, holder).setDuration(2000).start();
            }
        });

        findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, 100f);
                valueAnimator.setTarget(word);
                valueAnimator.setDuration(2000).start();
                valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        word.setTranslationY((Float) animation.getAnimatedValue());
                        word.setTranslationX((Float) animation.getAnimatedValue());
                    }
                });
            }
        });
    }

}
